package com.ncloud.resman.service.transform.impl;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ncloud.common.exception.UserException;
import com.ncloud.common.utils.DateUtils;
import com.ncloud.common.utils.excel.ExcelUtil;
import com.ncloud.resman.entity.Brand;
import com.ncloud.resman.mapper.DictMapper;
import com.ncloud.resman.service.BrandService;
import com.ncloud.resman.service.transform.BrandTransService;

@Service
public class BrandTransServiceImpl implements BrandTransService {
	
	private List<Map<String, String>> dictList = null;
	
	@Autowired
	private BrandService brandService;
	@Autowired
	private DictMapper dictMapper;

	@Override
	public void importBrand(Long userId, InputStream inputStream) {
		List<Object> brands = ExcelUtil.getInstance().readExcel2ObjsByInputStream(inputStream, Brand.class,1,0);
		// 翻译字典表
		for (Object object : brands) {
			Brand brand = (Brand)object;
			brand.setCreateUser(userId);
			brand.setUpdateUser(userId);
			brand.setCreateDate(new Date());
			brand.setUpdateDate(new Date());
			String eqptType = getValueByKey(brand.getEqptType());
			if(StringUtils.isBlank(eqptType)){
				throw new UserException("设备类型不存在匹配记录【设备类型："+brand.getEqptType()+"】");
			}
			brand.setEqptType(eqptType);
			brandService.insertBrand(brand);
		}
		dictList = null;
	}

	private String getValueByKey(String eqptType) {
		if(dictList ==null){
			dictList = dictMapper.getDictListById("eqpt_unit_type");
		}
		for (Map<String, String> map : dictList) {
			if(map.get("key").equals(eqptType))
				return map.get("value");
			if(map.get("value").equals(eqptType))
				return map.get("key");
		}
		return null;
	}

	@Override
	public void exportBrand(String eqptType, HttpServletResponse response,boolean isTemplate) throws IOException {
		if(eqptType!=null && StringUtils.isBlank(eqptType)) eqptType = null;
		List<Brand> brandList = new ArrayList<Brand>();
		if(!isTemplate){
			brandList = brandService.findBrandsByType(eqptType);
			// 翻译字典表
			brandList.parallelStream().forEach(brand->{
				brand.setEqptType(getValueByKey(brand.getEqptType()));
			});
		}
		dictList = null;
		OutputStream os = null;
		String title = "品牌列表"+(isTemplate?"模板":"");
		response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-disposition", "attachment;filename="+new String(title.getBytes("gb2312"), "ISO-8859-1")+".xls");
        os = response.getOutputStream();
		Map<String, String> map = new HashMap<String, String>();
        map.put("title", "品牌列表");
        map.put("total", brandList.size()+" 条");
        map.put("date", DateUtils.getDate());
		ExcelUtil.getInstance().exportObj2ExcelByTemplate(map, "brand-info-template.xls",
				os, brandList, Brand.class, true);
	}

}
